<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="服务期间" prop="servicePeriod">
        <el-input v-model="queryParams.servicePeriod" placeholder="请输入服务期间" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->
    <div>
      <span style="font-size: 18px;font-weight: 200;">仪器名称: &nbsp;{{instrumentationName}}</span>
      <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
        v-hasPermi="['system:instrumentationForeignService:edit']" style="margin-left: 200px;">新增</el-button>
      <el-button plain style="margin-left: 20px;" size="mini" icon="el-icon-arrow-left" @click="goBack()">返回</el-button>
    </div>

    <el-table v-loading="loading" :data="instrumentationForeignServiceList">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="编号" align="center" prop="id" width="60"/>
      <!-- <el-table-column label="填报记录" align="center" prop="fillRecord" /> -->
      <el-table-column label="服务开始日期" align="center" prop="servicePeriodStart" />
      <el-table-column label="服务结束日期" align="center" prop="servicePeriodEnd" />
      <el-table-column label="有效运行时长(小时)" align="center" prop="effectiveRunningTime" />
      <el-table-column label="对外服务时长(小时)" align="center" prop="externalServiceTime" />
      <el-table-column label="服务单位数(家)" align="center" prop="serviceUnitNumber" />
      <el-table-column label="服务科研单位数(家)" align="center" prop="serviceResearchUnitNumber" />
      <el-table-column label="服务企业数(家)" align="center" prop="serviceCompaniesNumber" />
      <el-table-column label="服务项次数(次)" align="center" prop="serviceItemNumber" />
      <el-table-column label="服务单位内部(次)" align="center" prop="serviceUnitInside" />
      <el-table-column label="服务单位外部(次)" align="center" prop="serviceUnitOutside" />
      <el-table-column label="检测样品数(个)" align="center" prop="samplesTestedNumber" />
      <el-table-column label="服务收入(万元)" align="center" prop="serviceRevenue" />
      <el-table-column label="创建时间" align="center" prop="createdTime" width="80">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createdBy" />
      <el-table-column label="操作" align="center" width="100">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:instrumentationForeignService:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:instrumentationForeignService:edit']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />



    <!-- 添加或修改仪器对外服务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="mini" :inline="true">
        <!-- <el-form-item label="实验仪器id" prop="instrumentationId">
          <el-input v-model="form.instrumentationId" placeholder="请输入实验仪器id" />
        </el-form-item> -->
        <el-form-item label="填报记录" prop="fillRecord" label-width="150px">
          <span class="flowStyle">{{form.fillRecord}}</span>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="服务开始日期" prop="servicePeriodStart" label-width="150px">
              <!-- <el-input v-model="form.servicePeriod" placeholder="请输入服务期间" /> -->
              <el-date-picker v-model="form.servicePeriodStart" type="month" placeholder="请选择服务开始日期" format="yyyy-MM"
                style="width: 165px;">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="服务结束日期" prop="servicePeriodEnd" label-width="150px">
              <!-- <el-input v-model="form.servicePeriod" placeholder="请输入服务期间" /> -->
              <el-date-picker v-model="form.servicePeriodEnd" type="month" placeholder="请选择服务结束日期" format="yyyy-MM"
                style="width: 165px;">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="有效运行时长(小时)" prop="effectiveRunningTime" label-width="150px">
              <el-input v-model="form.effectiveRunningTime" placeholder="请输入有效运行时长(小时)"
                oninput="value=value.replace(/[^\d\.]/g,'').replace('.','$#$').replace(/\./g,'').replace('$#$','.')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="对外服务时长(小时)" prop="externalServiceTime" label-width="150px">
              <el-input v-model="form.externalServiceTime" placeholder="请输入对外服务时长(小时)"
                oninput="value=value.replace(/[^\d\.]/g,'').replace('.','$#$').replace(/\./g,'').replace('$#$','.')" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="服务单位数(家)" prop="serviceUnitNumber" label-width="150px">
              <el-input v-model="form.serviceUnitNumber" placeholder="请输入服务单位数(家)"
                oninput="value=value.replace(/[^\d]/g,'')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="服务科研单位数(家)" prop="serviceResearchUnitNumber" label-width="150px">
              <el-input v-model="form.serviceResearchUnitNumber" placeholder="请输入服务科研单位数(家)"
                oninput="value=value.replace(/[^\d]/g,'')" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="服务企业数(家)" prop="serviceCompaniesNumber" label-width="150px">
              <el-input v-model="form.serviceCompaniesNumber" placeholder="请输入服务企业数(家)"
                oninput="value=value.replace(/[^\d]/g,'')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="服务项次数(次)" prop="serviceItemNumber" label-width="150px">
              <el-input v-model="form.serviceItemNumber" placeholder="请输入服务项次数(次)"
                oninput="value=value.replace(/[^\d]/g,'')" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="服务单位内部(次)" prop="serviceUnitInside" label-width="150px">
              <el-input v-model="form.serviceUnitInside" placeholder="请输入服务单位内部(次)"
                oninput="value=value.replace(/[^\d]/g,'')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="服务单位外部(次)" prop="serviceUnitOutside" label-width="150px">
              <el-input v-model="form.serviceUnitOutside" placeholder="请输入服务单位外部(次)"
                oninput="value=value.replace(/[^\d]/g,'')" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="检测样品数(个)" prop="samplesTestedNumber" label-width="150px">
              <el-input v-model="form.samplesTestedNumber" placeholder="请输入检测样品数(个)"
                oninput="value=value.replace(/[^\d]/g,'')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="服务收入(万元)" prop="serviceRevenue" label-width="150px">
              <el-input v-model="form.serviceRevenue" placeholder="请输入服务收入(万元)"
                oninput="value=value.replace(/[^\d\.]/g,'').replace('.','$#$').replace(/\./g,'').replace('$#$','.')" />
            </el-form-item>
          </el-col>
        </el-row>

        <!-- <el-form-item label="创建时间" prop="createdTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.createdTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择创建时间">
          </el-date-picker>
        </el-form-item> -->
        <!-- <el-form-item label="创建人" prop="createdBy">
          <el-input v-model="form.createdBy" placeholder="请输入创建人" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-hasPermi="['system:instrumentationForeignService:edit']">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listInstrumentationForeignService, getInstrumentationForeignService, delInstrumentationForeignService, addInstrumentationForeignService, updateInstrumentationForeignService, exportInstrumentationForeignService } from "@/api/labbase/instrumentationForeignService";
  import { formatCurrentDate } from "@/utils/index";
  import { getUserProfile } from "@/api/system/user";

  export default {
    name: "InstrumentationForeignService",
    data() {
      return {
        //用户对象
        user: {},
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 仪器对外服务表格数据
        instrumentationForeignServiceList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          instrumentationId: null,
          fillRecord: null,
          servicePeriod: null,
          effectiveRunningTime: null,
          externalServiceTime: null,
          serviceUnitNumber: null,
          serviceResearchUnitNumber: null,
          serviceCompaniesNumber: null,
          serviceItemNumber: null,
          serviceUnitInside: null,
          serviceUnitOutside: null,
          samplesTestedNumber: null,
          serviceRevenue: null,
          createdTime: null,
          createdBy: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        },
        // 实验室仪器id
        instrumentationId: null,
        // 实验室仪器名称
        instrumentationName: null
      };
    },
    created() {
      this.instrumentationId =this.$route.params.id,
      this.instrumentationName = this.$route.params.name,
      this.getList();
      this.getUserInfo();
    },
    methods: {
      //查询当前用户信息
      getUserInfo() {
        getUserProfile().then(response => {
          this.user = response.data;
        });
      },
      /** 查询仪器对外服务列表 */
      getList() {
        this.loading = true;
        this.queryParams.instrumentationId = this.instrumentationId
        listInstrumentationForeignService(this.queryParams).then(response => {
          this.instrumentationForeignServiceList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          instrumentationId: this.instrumentationId,
          fillRecord: formatCurrentDate() + this.user.nickName + "填报",
          servicePeriod: null,
          effectiveRunningTime: null,
          externalServiceTime: null,
          serviceUnitNumber: null,
          serviceResearchUnitNumber: null,
          serviceCompaniesNumber: null,
          serviceItemNumber: null,
          serviceUnitInside: null,
          serviceUnitOutside: null,
          samplesTestedNumber: null,
          serviceRevenue: null,
          createdTime: null,
          createdBy: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加仪器对外服务";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getInstrumentationForeignService(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改仪器对外服务";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateInstrumentationForeignService(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addInstrumentationForeignService(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除仪器对外服务编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delInstrumentationForeignService(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有仪器对外服务数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportInstrumentationForeignService(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      },
      // 返回
      goBack() {
        this.$router.go(-1)
      }
    }
  };
</script>
<style type="text/css" scoped>
  /* /deep/ .el-form-item {
    width: 150px;
  } */
  /deep/ .el-table .el-table__header-wrapper th, .el-table .el-table__fixed-header-wrapper th {
    height: 10px;
    font-size: 12px;
    font-weight: 500;
  }
</style>
